﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using M;
using Mysoft.MyCM;
using Mysoft.MyCM.Scm;
using Mysoft.MyCM.Business;
using Mysoft.MyCM.Business.Scm;

namespace Mysoft.MyCM.Web.Controllers
{
	public class ScmController : MFunViewBaseController
	{
		/// <summary>
		/// SCM客户首页
		/// </summary>
		/// <returns></returns>
		public ActionResult Index()
		{
			CustomBLL cstBLL = new CustomBLL();
			ProjectBLL projBLL = new ProjectBLL();
			// 获取客户详细信息
			Custom2 cst = cstBLL.GetCustom2(this.RequestParam.SelectedCustomerCode);
			// 获取客户项目信息
			List<Project2> projects = projBLL.GetCustomProjects2(cst.CustomGUID);
			// 调整客户的分支顺序
			projects.ForEach(proj2 =>
			{
				proj2.Branches = proj2.Branches.OrderBy(p => p.BranchNum).ToList();
			});

			dynamic model = new ExpandoObject();
			model.Customer2 = cst;
			model.Projects2 = projects;

			// TODO: 处理无数据部分

			// 设置标题
			ViewBag.Title = string.Format("{0} - 研发配置信息", cst.CustomName);

			return View(model);
		}

		/// <summary>
		/// 初始化 GET
		/// </summary>
		/// <returns></returns>
		public ActionResult Init()
		{
			// TODO: Configuration Initialization
			throw new M.MException("Configuration Initialization Coming soon...", M.MExceptionLevel.Stop);

			ViewBag.CustomCode = this.RequestParam.SelectedCustomerCode;
			return View();
		}

		/// <summary>
		/// 授权 GET
		/// </summary>
		/// <param name="param">不带参数，则为客户ProjectCollection初始化；带参数则为分支ID</param>
		/// <returns></returns>
		public ActionResult Authorize(Guid? param)
		{
			// TODO: Branch Extented Authorization
			throw new M.MException("Branch Extented Authorization Coming soon...", M.MExceptionLevel.Stop);


			ViewBag.AuthorizeType = "ProjectCollection";
			ViewBag.BranchId = "";
			if (param.HasValue)
			{
				ViewBag.AuthorizeType = "Branch";
				ViewBag.BranchId = param.Value;
			}
			return View();
		}

		/// <summary>
		/// 更新配置信息
		/// </summary>
		/// <param name="param">项目ID</param>
		/// <returns></returns>
		public ActionResult Update(Guid param)
		{
			// TODO:  Configuration Update
			throw new M.MException("Configuration Update Coming soon...", M.MExceptionLevel.Stop);

			ViewBag.ProjectId = param;
			return View();
		}

		/// <summary>
		/// 从主干新建分支
		/// </summary>
		/// <param name="param">项目ID</param>
		/// <returns></returns>
		public ActionResult NewBranch(Guid param)
		{
			// TODO:  Adding Extented Branch
			throw new M.MException("Adding Extented Branch Coming soon...", M.MExceptionLevel.Stop);

			ViewBag.ProjectId = param;
			return View();
		}

		/// <summary>
		/// 删除分支
		/// </summary>
		/// <param name="param">分支ID</param>
		/// <returns></returns>
		public ActionResult DeleteBranch(Guid param)
		{
			// TODO:  Removing Branch
			throw new M.MException("Removing Branch Coming soon...", M.MExceptionLevel.Stop);

			ViewBag.BranchId = param;
			return View();
		}

		/// <summary>
		/// 查看历史版本树
		/// </summary>
		/// <param name="param">分支ID</param>
		/// <returns></returns>
		public ActionResult Version(Guid param)
		{
			// TODO:  Version Tree View
			throw new M.MException("Version Tree View Coming soon...", M.MExceptionLevel.Stop);

			ViewBag.BranchId = param;
			return View();
		}

		/// <summary>
		/// 查看配置任务记录
		/// </summary>
		/// <returns></returns>
		public ActionResult Job()
		{
			// TODO:  Job View
			throw new M.MException("Job View Coming soon...", M.MExceptionLevel.Stop);
			return View();
		}

		/// <summary>
		/// 查看更新池
		/// </summary>
		/// <returns></returns>
		public ActionResult Updates()
		{
			// TODO:  Update Package Pool View
			throw new M.MException("Update Package Pool View Coming soon...", M.MExceptionLevel.Stop);
			return View();
		}

		/// <summary>
		/// 查看服务器信息
		/// </summary>
		/// <returns></returns>
		public ActionResult Servers()
		{
			// TODO:  Servers Status View
			throw new M.MException("Servers Status View Coming soon...", M.MExceptionLevel.Stop);

			return View();
		}
	}
}